package com.assessment.service.impl;

import com.assessment.entity.*;
import com.assessment.mapper.AcomresMapper;
import com.assessment.mapper.AitemavgMapper;
import com.assessment.service.AcomresService;
import com.assessment.service.AitemavgService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import static com.assessment.utils.Constant.SUBMIT;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鑫十一
 * @since 2021-12-15
 */
@Service
@Transactional
public class AcomresServiceImpl extends ServiceImpl<AcomresMapper, Acomres> implements AcomresService {

    @Autowired(required = false)
    private AitemavgMapper aitemavgMapper;
    @Autowired(required = false)
    private AitemavgService aitemavgService;

    @Override
    public List<Column> getColumns() {
        List<Column> columns = new ArrayList<>();
        columns.add(new Column().setLabel("计算名").setProp("name"));
        columns.add(new Column().setLabel("所属考核").setProp("aname"));
        columns.add(new Column().setLabel("考核对象").setProp("pname"));
        columns.add(new Column().setLabel("自评").setProp("self"));
        columns.add(new Column().setLabel("互评").setProp("diff"));
        columns.add(new Column().setLabel("服务对象").setProp("service"));
        columns.add(new Column().setLabel("校领导").setProp("leader"));
        columns.add(new Column().setLabel("分数").setProp("score"));
        return columns;
    }
    private String getAvgScore(String pid, String[] strings){
        DecimalFormat df = new DecimalFormat("#.00");
        QueryWrapper<Aitemavg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        queryWrapper.in("itid", strings);
        List<Aitemavg> aitemavgs = aitemavgMapper.selectList(queryWrapper);
        final double[] score = {0.0};
        final int[] sum = {0};
        aitemavgs.forEach(aitemavg -> {
            if (!aitemavg.getScore().equals("暂无")){
                sum[0] = sum[0] + 1;
                score[0] = score[0] + Double.parseDouble(aitemavg.getScore());
            }
        });
        return sum[0] == 0? "0": String.valueOf(df.format(score[0]/sum[0]));
    }

    @Override
    public List<AcomresVo> getVolist(QueryWrapper<Acomres> queryWrapper) {
        aitemavgService.computeAvg();
        List<AcomresVo> acomresVos = new ArrayList<>();
        List<Acomres> list = this.list(queryWrapper);
        list.forEach(acomres -> {
            // 获取自评分数
            String selfAvgScore = getAvgScore(acomres.getPid(), new String[]{"1477556092805337089", "1477561398275952642"});
            String diffAvgScore = getAvgScore(acomres.getPid(), new String[]{"1477561726815784961", "1477561857279610881"});
            String serviceAvgScore = getAvgScore(acomres.getPid(),new String[]{"1477562158057345026", "1477562403365408770", "1477562805473333249", "1477562999044657153"});
            String leaderAvgScore = getAvgScore(acomres.getPid(),new String[]{"1481495165907509250", "1481495305896599553"});
            acomresVos.add(new AcomresVo().setId(acomres.getId()).setAid(acomres.getAid())
                    .setAname(acomres.getAname()).setName(acomres.getName())
                    .setPid(acomres.getPid()).setPname(acomres.getPname())
                    .setScore(acomres.getScore())
                    .setSelf(selfAvgScore)
                    .setDiff(diffAvgScore)
                    .setLeader(leaderAvgScore)
                    .setService(serviceAvgScore));
        });
        return acomresVos;
    }
}
